define(function(require, exports, module) {
	var $ = jQuery = require('jquery');
	var Dialog = require('dialog');
	var Confirmbox = require('confirmbox');
	var Validator = require('validator').extend({
		attrs:{
			itemClass:'m-form-item',
			explainClass:'m-form-explain',
			itemErrorClass:'m-form-item-error',
			displayHelper: function (item) {
          		var labeltext = $(item.element).parent().parent().find('.m-form-item-label').text();
				if(labeltext) {
				 	labeltext = labeltext.replace(/^[\*\s\:\：]*/, '').replace(/[\*\s\:\：]*$/, '');
				}
				name = item.element.attr('name');
           		return labeltext || name;
        	}
		}
	});
	
	
	
	var GLOBAL = exports;

	/**
	 * 
	 */
	GLOBAL.parseUri = function(uri, appendEasyFormName, params) {
		var r = window.baseURI;
		if (uri.charAt(0) != '/') {
			r += '/';
		}
		r += uri;
		if (appendEasyFormName) {
			var p = "easyForm=" + __easyFormName;
			r = appandParams(r, p)
		}
		if (params && params.length > 0) {
			r = appandParams(r, params);
		}
		return r;
	}

	function appandParams(uri, params) {
		var rpa = params;
		if (params.charAt(0) == '&') {
			rpa = params.substring(1);
		}
		if (uri.indexOf("?") > -1) {
			// user/new?a=1
			return uri + "&" + rpa;
		} else {
			// user/new
			return uri + "?" + rpa;
		}
	}

	function selectCheck(selectedNum) {
		var g = window.dataPanelGrid;
		var selected = g.getSelectRecord();
		var value = selected.value;
		var v = selected.name;

		if (value.length == 0) {
			Confirmbox.alert('请选择记录.');
			return false;
		}
		var paramArray = [];
		if (selectedNum == '1') {
			if (value.length != 1) {
				Confirmbox.alert('请选择一条记录.');
				return false;
			}
			paramArray.push(v + '=' + value[0]);
		} else if (selectedNum == 'n') {
			for ( var i = 0; i < value.length; i++) {
				paramArray.push(v + '=' + value[i]);
			}
		}
		return paramArray;
	}
	
	GLOBAL.createValidator = function(formEl) {
		return new Validator({
			element:formEl
		});
	}
	
	
	GLOBAL.effectEvent = function(eventArray) {
		if (eventArray) {
			for ( var i = 0; i < eventArray.length; i++) {
				this.effectEventDetail(eventArray[i])
			}
		}
	}

	GLOBAL.effectEventDetail = function(ev) {
		var el = "input[name='" + ev.name + "']";
		$(el).bind(ev.event, function() {
			eval("GLOBAL." + ev.func)
		});
	}

	GLOBAL.newRecord = function() {
		this.renderWindow("/new");
	}

	GLOBAL.editRecord = function() {
		this.renderWindow("/edit", "1");
	}

	GLOBAL.deleteRecord = function() {
		this.ajaxCommand("/delete", "确定删除吗?", "n");
	}

	/**
	 * = 1 | > 1 edit?
	 */
	GLOBAL.renderWindow = function(uri, selectedNum) {
		var dialog = new Dialog({
			width : '800px',
			height : '800px',
			hasMask : {
				hideOnClick : false
			}
		});
		if (selectedNum) {
			var selected = selectCheck(selectedNum);
			if (!selected) {
				return;
			}
			var u = this.parseUri(uri, true, selected.join("&"));
			dialog.set('content', u);
		} else {
			dialog.set('content', this.parseUri(uri, true));
		}
		dialog.show();
	}

	GLOBAL.ajaxCommand = function(uri, confirmMsg, selectedNum) {
		var u = uri;
		if (selectedNum) {
			var selected = selectCheck(selectedNum);
			if (!selected) {
				return;
			}
			u = this.parseUri(uri, true, selected.join("&"));
		}
		Confirmbox.confirm(confirmMsg, "请确认", function() {
			$.post(u, function() {
				window.refreshGrid();
			});
		});
	}

});